package com.msb.search.mq;

import com.alibaba.fastjson2.JSON;
import com.msb.common.constart.RabbitMQConstants;
import com.msb.common.model.StandardSubmit;
import com.msb.search.service.SearchService;
import com.msb.search.utils.SearchUtils;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;


/**
 * @Author: lmf
 * @Create: 2025/3/19 21:05
 * @module
 */
@Component
@Slf4j
public class SmsWriteLogListener {
    @Resource
    private SearchService searchService;

    @RabbitListener(queues = RabbitMQConstants.SMS_LOG_ES)
    public void consume(StandardSubmit submit, Channel channel, Message msg) throws IOException {
        log.info("短信日志写入ES成功：{}", submit);
        log.info("Tag:{}", msg.getMessageProperties().getDeliveryTag());

        searchService.index(SearchUtils.getYearIndex(),submit.getSequenceId().toString(), JSON.toJSONString(submit));

        // 手动确认
        channel.basicAck(msg.getMessageProperties().getDeliveryTag(), false);
    }
}
